Java Technologies Layout Management Example: BorderPane, HBox, VBox এর ব্যবহার গাইড ও নোট

392

Layout Management Example in Java: BorderPane, HBox, VBox

JavaFX provides several layout managers that help in organizing UI components in a structured manner. Among them, BorderPane, HBox, and VBox are some of the most commonly used layout managers. Below are examples demonstrating how to use each of these layout managers.

১. BorderPane Layout Example

BorderPane is a layout manager that divides the screen into five regions: top, bottom, left, right, and center. You can place different components (controls) in these regions.

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;

public class BorderPaneExample extends Application {
    @Override
    public void start(Stage primaryStage) {
        // Create a BorderPane layout
        BorderPane borderPane = new BorderPane();

        // Create buttons for each region
        Button topButton = new Button("Top");
        Button bottomButton = new Button("Bottom");
        Button leftButton = new Button("Left");
        Button rightButton = new Button("Right");
        Button centerButton = new Button("Center");

        // Add buttons to their respective positions in the BorderPane
        borderPane.setTop(topButton);
        borderPane.setBottom(bottomButton);
        borderPane.setLeft(leftButton);
        borderPane.setRight(rightButton);
        borderPane.setCenter(centerButton);

        // Create a Scene and add the BorderPane layout
        Scene scene = new Scene(borderPane, 400, 300);

        // Set the stage with the scene and display it
        primaryStage.setTitle("BorderPane Example");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
}

২. HBox Layout Example

HBox arranges the UI components horizontally. It places children next to each other, from left to right, by default.

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;

public class HBoxExample extends Application {
    @Override
    public void start(Stage primaryStage) {
        // Create an HBox layout
        HBox hBox = new HBox(10); // 10 is the spacing between the buttons

        // Create buttons
        Button button1 = new Button("Button 1");
        Button button2 = new Button("Button 2");
        Button button3 = new Button("Button 3");

        // Add buttons to the HBox
        hBox.getChildren().addAll(button1, button2, button3);

        // Create a Scene and add the HBox layout
        Scene scene = new Scene(hBox, 300, 100);

        // Set the stage with the scene and display it
        primaryStage.setTitle("HBox Example");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
}

৩. VBox Layout Example

VBox arranges the UI components vertically. The components are stacked on top of each other, from top to bottom.

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class VBoxExample extends Application {
    @Override
    public void start(Stage primaryStage) {
        // Create a VBox layout
        VBox vBox = new VBox(10); // 10 is the spacing between the buttons

        // Create buttons
        Button button1 = new Button("Button 1");
        Button button2 = new Button("Button 2");
        Button button3 = new Button("Button 3");

        // Add buttons to the VBox
        vBox.getChildren().addAll(button1, button2, button3);

        // Create a Scene and add the VBox layout
        Scene scene = new Scene(vBox, 200, 150);

        // Set the stage with the scene and display it
        primaryStage.setTitle("VBox Example");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
}

৪. Combining BorderPane, HBox, and VBox

You can combine BorderPane, HBox, and VBox to create more complex layouts. For example, placing a VBox inside the Center region of a BorderPane and an HBox in the Bottom region.

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class CombinedLayoutExample extends Application {
    @Override
    public void start(Stage primaryStage) {
        // Create BorderPane layout
        BorderPane borderPane = new BorderPane();

        // Create a VBox for the center region
        VBox vBox = new VBox(10);
        Button vboxButton1 = new Button("VBox Button 1");
        Button vboxButton2 = new Button("VBox Button 2");
        vBox.getChildren().addAll(vboxButton1, vboxButton2);

        // Create an HBox for the bottom region
        HBox hBox = new HBox(10);
        Button hboxButton1 = new Button("HBox Button 1");
        Button hboxButton2 = new Button("HBox Button 2");
        hBox.getChildren().addAll(hboxButton1, hboxButton2);

        // Set VBox in the center region of BorderPane
        borderPane.setCenter(vBox);

        // Set HBox in the bottom region of BorderPane
        borderPane.setBottom(hBox);

        // Create a Scene and add the BorderPane layout
        Scene scene = new Scene(borderPane, 400, 300);

        // Set the stage with the scene and display it
        primaryStage.setTitle("Combined Layout Example");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
}

ব্যাখ্যা:

  1. BorderPane: এই লেআউটটি পাঁচটি অঞ্চলে বিভক্ত: Top, Bottom, Left, Right, এবং Center। আপনি বিভিন্ন উপাদানকে এই অঞ্চলে রাখতে পারেন।
  2. HBox: HBox উপাদানগুলোকে অনুভূমিকভাবে সাজায়। উপাদানগুলি একে অপরের পাশে দেখা যায়, এবং আপনি স্পেসিং নিয়ন্ত্রণ করতে পারেন।
  3. VBox: VBox উপাদানগুলোকে উল্লম্বভাবে সাজায়, অর্থাৎ উপাদানগুলি একে অপরের উপরে থাকে। এখানে আপনি উপাদানগুলির মধ্যে স্পেসিংও নিয়ন্ত্রণ করতে পারেন।

এই উদাহরণগুলো JavaFX লেআউট ব্যবস্থাপনা এবং কিভাবে বিভিন্ন লেআউট ব্যবহার করা যায় তা বুঝতে সাহায্য করবে।

Content added By
Promotion

Are you sure to start over?

Loading...